package com.danton.model;

import java.util.List;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

@SuppressWarnings("serial")
public class BsPermissionModel extends Model<BsPermissionModel> {
	public static String RESOURCE = "resource";
	public static String DESCRIPTION = "description";
	public static String REMARK = "remark";
	
	public static final BsPermissionModel dao = new BsPermissionModel();

	public List<BsPermissionModel> findPermissionsByRoleID(int roleID) {
		return super.find("select * from bs_permission bp join bs_role_permission brp on bp.id = brp.permission_id where brp.role_id=?", roleID);
	}
	
	public List<BsPermissionModel> findPermissionByRoleID(int roleID) {
		return super.find("select brp.permission_id,bp.module,bp.description from bs_permission bp join bs_role_permission brp on bp.id = brp.permission_id where brp.role_id=?", roleID);
	}
	public List<BsPermissionModel> findPermissionsByRoleIDs(List<Integer> idList) {
		String ids = idList.toString().replace("[", "(").replace("]", ")");
		return super.find("select * from bs_permission bp join bs_role_permission brp on bp.id = brp.permission_id where brp.role_id in " + ids);
	}
	/*
	 * 查询所有的模块列表
	 * */
	public List<Record> listAllModule() {
		return Db.find("select * from bs_permission group by module");
	}
	/*
	 * 查询角色已有的模块列表
	 * */
	public List<Record> listRoleModule(int role_id) {
		return Db.find("select bp.* from bs_permission bp join bs_role_permission brp on bp.id = brp.permission_id where brp.role_id=? group by bp.module", role_id);
	}
	/*
	 * 查询所有的权限列表,通过模块
	 * */
	public List<BsPermissionModel> listAllPermissionByModule(String module) {
		return super.find("select * from bs_permission where module=? order by module",module);
	}
	/*
	 * 查询已关联的权限列表,通过模块
	 * */
	public List<BsPermissionModel> listRolePermissionByModule(int role_id,String module) {
		return super.find("select brp.*,bp.* from bs_permission bp join bs_role_permission brp on bp.id = brp.permission_id where brp.role_id=? and bp.module=?", role_id,module);
	}
	public Page<BsPermissionModel> paginate(int pageNumber, int pageSize){
		return super.paginate(pageNumber, pageSize, "select *,id as permission_id", "from bs_permission order by id desc");
	}
}
